<template>
    <div>
        <el-form :model="formDB" label-width="80px" ref="AddAndUpFinanceref" :rules="rule">
            <el-row>
                <el-col :span="9" :offset="2">
                    <el-form-item label="员工姓名"   prop="CustomerId">
                        <el-select filterable name="CustomerId" id="CustomerId" v-model="formDB.CustomerId" class="SeW">
                            <el-option value="0" label="无"></el-option>
                            <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName" v-text="v.UserName"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="9" :offset="2">
                    <el-form-item label="应发工资" prop="Money">
                        <el-input type="text" id="Money" name="Money" v-model="formDB.Money"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="9" :offset="2">
                    <el-form-item label="个税" prop="PersonalTax">
                        <el-input type="text" id="PersonalTax" name="PersonalTax" v-model="formDB.PersonalTax"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="9" :offset="2">
                    <el-form-item label="工资时间" prop="CollectionDate">
                        <el-date-picker style="width: 100%"  v-model="formDB.CollectionDate"
                                        class="SeW"
                                        value-format="yyyy-MM-dd"
                                        format="yyyy-MM-dd"
                                        type="date"
                                        placeholder="工资时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="9" :offset="2">
                    <el-form-item label="项目"   prop="ProjectId">
                        <el-select filterable  name="ProjectId" v-model="formDB.ProjectId" :popper-append-to-body="true" class="SeW">
                            <el-option :value="0" label="无"></el-option>
                            <el-option v-for="(v,k) in AllProject" :value="v.Id" :data-userid="v.Id" :label="v.ProjectName" v-text="v.ProjectName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="9" :offset="2">
                    <el-form-item label="备注" prop="Remark">
                        <el-input type="text" id="Remark" v-model="formDB.Remark" name="Remark"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row style="text-align:center">
                <el-col>
                    <el-button type="primary" @click="SaveFinance">提交</el-button>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
    // import * as publicjs from "/public/Scriput/Public"
    export default {
        name: "AddAndUpFinance",
        props: {
            AllCustomer: {
                type: Array,
                required: true
            },
            AllUsers: {
                type: Array,
                required: true
            },
            AllProject: {
                type: Array,
                required: true
            },
            AllFinanceBankCard: {
                type: Array,
                required: true
            },
            btnNumber: {
                type: Number,
                required: true,
            },
            formDB: {
                type: Object,
                required: true,
            }
        },
        data() {
            var demicalcheck=(rule, value, callback)=>{
                if (!value){
                    return callback(new Error('不能为空'));
                }else{
                    let pattern = /^[0-9]+([.]{1}[0-9]+){0,1}$/;
                    if (pattern.test(value)) {
                        callback();
                    } else {
                        callback(new Error('必须数字'));
                    }
                }
            }
            return {
                // formDB: {
                //      CollectionType: '',
                //      CollectionDate: '',
                //      ProjectId: '',
                //      CustomerId: '',
                //      CollectionMode: '',
                //      FinanceBankId: '',
                //      Money: '',
                //      Remark: '',
                // },
                rule:{
                    CollectionDate:[ { required: true, message: '请选择收款时间', trigger: 'change' }],
                    ProjectId:[ { required: true, message: '请选择项目', trigger: 'change' }],
                    CustomerId:[ { required: true, message: '请选择单位', trigger: 'change' } ],
                    Money:[ { validator: demicalcheck, trigger: 'blur' }],
                }
            }
        },
        created() {
            console.log(this.formDB)
        },
        methods: {
            SaveFinance() {
                let _this = this;
                this.$refs['AddAndUpFinanceref'].validate(vali=>{
                    if (vali){
                        //添加
                        if (this.btnNumber == 1) {
                            let loadding = this.openLoading();
                            this.$http.post('/Finance/AddFinanceCollection', {
                                finance: this.formDB
                            }).then((result) => {
                                loadding.close();
                                if (result) {
                                    this.$message.success('信息提交成功');
                                } else {
                                    this.$message.error('信息提交失败');
                                }
                                _this.$parent.$parent.AddFinanceShow = false;
                                console.log(_this.$parent.$parent);
                                _this.$parent.$parent.SelFinance();
                            });
                        } else {
                            //信息修改
                            let loadding = this.openLoading();
                            this.$http.post('/Finance/UpdateFinanceCollection', {
                                finance: this.formDB
                            }).then((result) => {
                                loadding.close();
                                if (result) {
                                    this.$message.success('信息提交成功');
                                } else {
                                    this.$message.error('信息提交失败');
                                }
                                _this.$parent.$parent.AddFinanceShow = false;
                                _this.$parent.$parent.SelFinance();
                            });
                        }
                    }
                })
            },
            //支付方式切换
            CollectionModeChangeEvent(){
                this.formDB.FinanceBankId=0
            },
            //ResultBit(bit) {
            //    if (bit) {
            //        this.$message.success('信息提交成功');
            //    }else{
            //        this.$message.error('信息提交失败');
            //    }
            //    this.$parent.$parent.AddFinanceShow = false;
            //    this.$parent.$parent.SelFinance();
            //},
        }
    }
</script>

<style scoped>
    .SeW {
        width: 100%;
    }
</style>
